﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace LinkCard.AdminCP.AboutUs
{
    public partial class banner_edit : System.Web.UI.Page
    {
        public AppEnv _Instance = AppEnv.Instance();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
                {
                    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
                    {
                        Response.Redirect("/message_box.aspx?why=1&msg=5");
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                DataBindPaging();
                divSucc.Visible = false;
            }

        }

        private void DataBindPaging()
        {

            try
            {
                DataTable dt = DataHelper.SelectTable("Select * from banner order by position",false);
                msgList.DataSource = dt;
                msgList.DataBind();

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["ID"].ToString();

            var txtTitle = (TextBox)e.Item.FindControl("txtTitle");
            var txtLink = (TextBox)e.Item.FindControl("txtLink");
            var txtFilePath = (TextBox)e.Item.FindControl("txtFilePath");
            var uploadify = (FileUpload)e.Item.FindControl("uploadify");
            var chkIsActive = (CheckBox)e.Item.FindControl("isActive");

            txtFilePath.CssClass = "cssFilePath" + e.Item.ItemIndex;
            uploadify.CssClass = "cssUploadify" + e.Item.ItemIndex;

            txtTitle.Text = row["Title"].ToString();
            txtLink.Text = row["Link"].ToString();
            txtFilePath.Text = row["FilePath"].ToString();
            chkIsActive.Checked =  ConvertUtility.ToBoolean(row["isActive"]);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in msgList.Items)
            {
                var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                var txtTitle = (TextBox)item.FindControl("txtTitle");
                var txtLink = (TextBox)item.FindControl("txtLink");
                var txtFilePath = (TextBox)item.FindControl("txtFilePath");
                var chkIsActive = (CheckBox)item.FindControl("isActive");

                string sSQL = "";

                if (chkDelete.Checked)
                {
                    int nIsActive = 0;
                    if (chkIsActive.Checked) nIsActive = 1;
                    sSQL = "Update banner set Title = '" + hash1way.SQLPatch(txtTitle.Text) + "', link = '" + hash1way.SQLPatch(txtLink.Text) + "',FilePath = '" + hash1way.SQLPatch(txtFilePath.Text) + "', isActive = " + nIsActive + " where ID=" + chkDelete.Value;
                    DataHelper.ExecuteNonQuery(sSQL, true);
                }
            }
            divSucc.Visible = true;
        }
    }
}